package com.itboye.jmsfhz.app.ui.web;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.MainApplication;
import com.itboye.jmsfhz.app.logincontroller.LoginController;
import com.itboye.jmsfhz.data.presenter.GetYanzhengMaPresenter;
import com.itboye.jmsfhz.data.presenter.Projectpresenter;

public class WebProjectfaremgnAvtivity extends BaseActivity implements
		Observer, OnClickListener {
	private WebView webview;
	private TextView txt_title;
	private String url;
	private String title;
	private ProgressBar myProgressBar;
	Button btn_send;

	Projectpresenter projectpresenter;

	String uid;
	String id;
	private String role;

	// private LinearLayout linWeb;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.webactivity_jiuye;
	}

	@Override
	public void onMyCreate() {
		role= (String) SPUtils.get(this, null,
				SpPublicParams.UID, "");
		btn_send.setText("投标");
		title = getIntent().getStringExtra("title");
		txt_title.setText(title);
		url = getIntent().getStringExtra("url");
		id = getIntent().getStringExtra("id");
		webview.setVisibility(View.VISIBLE);
		projectpresenter=new Projectpresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		// linWeb.setVisibility(View.GONE);
		myProgressBar = new ProgressBar(this);
		judgeIsLogin();
		
		if (url == null) {
			btn_send.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
		}
		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if (newProgress == 100) {
					myProgressBar.setVisibility(View.INVISIBLE);
				} else {
					if (View.INVISIBLE == myProgressBar.getVisibility()) {
						myProgressBar.setVisibility(View.VISIBLE);
					}
					myProgressBar.setProgress(newProgress);
				}
				super.onProgressChanged(view, newProgress);
			}

		});
		webview.loadUrl(url);
	}
	public void judgeIsLogin() {

		String LoginUid = SPUtils.get(MainApplication.context, null,
				SpPublicParams.ROLE, "") + "";
		if(LoginUid.equals("6")){
			btn_send.setVisibility(View.VISIBLE);
		}else if(LoginUid.equals("5")){
			btn_send.setVisibility(View.GONE);
		}
		else {
			btn_send.setVisibility(View.GONE);
		}
		
	}

	public void imgback(View view) {
		this.finish();
	}

	@SuppressWarnings("static-access")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					projectpresenter.ProjectUpdateSuccessEvent)) {
				ByAlert.alert(""+entity.getData());
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_send:
//			LoginController.getProject();
			projectpresenter.getProject(uid, id);
			break;

		default:
			break;
		}
	}

}
